package com.yunji.cabinet.business;

import com.yunji.base.cache.BaseCache;
import com.yunji.base.domain.FeedbackDO;
import com.yunji.base.dto.AgentDTO;
import com.yunji.base.service.FeedbackService;
import com.yunji.cabinet.bo.CabinetBO;
import com.yunji.cabinet.bo.CabinetSelectQueryBo;
import com.yunji.cabinet.consumer.CabinetConsumer;
import com.yunji.cabinet.domain.ContainerDO;
import com.yunji.cabinet.service.ICabinetService;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.model.dto.FeedbackDTO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.model.dto.MemberFeedbackDTO;
import com.yunji.common.model.enums.FeedbackStatus;
import com.yunji.common.model.params.FeedbackParam;
import com.yunji.common.model.vo.FeedbackExportVO;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.message.business
 * @Title: FeedbackBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-30 09:30:04      V1.0        HuaAo       新建类
 */
@Slf4j
@Service
public class FeedbackBusiness {

    @Resource
    CabinetConsumer consumer;

    @Resource
    FeedbackService feedbackService;

    @Resource
    ICabinetService cabinetService;

    /**
     * @param dto
     * @Title addMemberFeedback
     * @Desc 添加用户反馈
     * @Date 2024-10-30 10:21:57.076
     * @Return boolean
     */
    public boolean addMemberFeedback(MemberFeedbackDTO dto) {
        FeedbackDO create = new FeedbackDO();
        // 故障基础信息
        create.setContent(dto.getContent());
        create.setAlbumPics(dto.getAlbumPics());

        // 反馈用户信息
        LoginInfo user = SystemContext.getUser();
        create.setUserId(user.getUserId());
        create.setUserType(UserType.USER.getCode());
        create.setUserName(user.getNickname());
        create.setPhoneNumber(user.getPhone());

        // 货柜信息
        String code = dto.getQrcode();
        log.info("【货柜商品】二维码信息:{}", code);
        CabinetBO bo = cabinetService.select(CabinetSelectQueryBo.builder().qrcode(code).build());
        Assert.isTrue(BeanUtils.isNotNull(bo), "未找到货柜信息");
        ContainerDO cabinet = bo.getCabinet();
        create.setQrcode(dto.getQrcode());
        create.setContainerId(cabinet.getContainerId());
        create.setChipImei(cabinet.getChipImei());
        create.setContainerName(cabinet.getContainerName());

        // 网点信息
        SiteDO siteInfo = BaseCache.getSite(cabinet.getSiteId());
        Assert.isTrue(BeanUtils.isNotNull(siteInfo), "未找到网点信息");
        create.setSiteId(siteInfo.getId());
        create.setSiteName(siteInfo.getSiteName());
        create.setSiteTel(siteInfo.getSiteTel());

        AgentDTO agentDTO = consumer.selectAgent(cabinet.getAgentId());
        Assert.isTrue(BeanUtils.isNotNull(agentDTO), "未找到代理信息");
        create.setAgentId(agentDTO.getAgentId());
        create.setAgentName(agentDTO.getAgentName());
        create.setAgentTel(agentDTO.getAgentTel());

        return feedbackService.create(create);
    }

    /**
     * @param param 查询参数
     * @Title search
     * @Desc 反馈列表
     * @Date 2024-10-30 11:52:42.343
     * @Return List<Feedback>
     */
    public List<FeedbackDO> searchList(FeedbackParam param) {
        return feedbackService.searchList(param);
    }

    /**
     * @param param
     * @Title export
     * @Desc 导出列表
     * @Date 2024-11-05 11:51:13.441
     * @Return List<Feedback>
     */
    public List<FeedbackExportVO> export(FeedbackParam param) {
        return feedbackService.search(param);
    }

    /**
     * @param dto
     * @Title handle
     * @Desc 处理反馈
     * @Date 2024-10-30 16:23:19.711
     * @Return boolean
     */
    public boolean handle(FeedbackDTO dto) {
        FeedbackDO feedback = feedbackService.select(dto.getFeedbackId());
        Assert.isTrue(BeanUtils.isNotNull(feedback), "未找到反馈单");
        Integer status = feedback.getFeedbackStatus();
        Assert.isTrue(BeanUtils.equals(status, FeedbackStatus.WAIT_DEAL.getCode(), FeedbackStatus.LAZY_DEAL.getCode()), "当前状态不可处理");
        if (Objects.equals(FeedbackStatus.ALREADY_DEAL.getCode(), dto.getFeedbackStatus())) {
            dto.setDealTime(TimeUtils.getNowDate());
        }
        return feedbackService.update(dto);
    }

    /**
     * @param id 反馈ID
     * @Title getInfo
     * @Desc 查询反馈详情
     * @Date 2024-10-30 16:47:19.937
     * @Return Feedback
     */
    public FeedbackDO getInfo(Long id) {
        return feedbackService.select(id);
    }

    /**
     * @param ids
     * @Title delete
     * @Desc 批量删除反馈
     * @Date 2024-10-31 13:58:37.287
     * @Return boolean
     */
    public boolean delete(List<Long> ids) {
        for (Long id : ids) {
            FeedbackDO feedback = feedbackService.select(id);
            Assert.isTrue(Objects.equals(feedback.getFeedbackStatus(), FeedbackStatus.ALREADY_DEAL.getCode()), "该状态无法删除");
        }
        return feedbackService.deleteByIds(ids);
    }

    public List<String> searchSiteName(String siteName) {
        FeedbackParam param = new FeedbackParam();
        param.setSearchField("site_name");
        param.setSiteName(siteName);
        return feedbackService.searchTableField(param);
    }

    public List<String> searchSiteTel(String siteTel) {
        FeedbackParam param = new FeedbackParam();
        param.setSearchField("site_tel");
        param.setSiteTel(siteTel);
        return feedbackService.searchTableField(param);
    }

    public List<String> searchAgentName(String agentName) {
        FeedbackParam param = new FeedbackParam();
        param.setSearchField("agent_name");
        param.setAgentName(agentName);
        return feedbackService.searchTableField(param);
    }


}